package executor

import (
	"fmt"
	"os"
	"zlash/config"
	C "zlash/constant"
)

func readConfig(path string) ([]byte, error) {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return nil, err
	}
	data,err := os.ReadFile(path)
	if err != nil{
		return nil ,err
	}
	if len(data) ==0 {
		return nil,fmt.Errorf("configuration file %s is empty",path)
	}
	return data,err
}

func Parse() (*config.Config, error) {
	return ParseWithPath(C.Path.Config())
}

func ParseWithPath(path string) (*config.Config, error) {
	buf, err := readConfig(path)
	if err != nil {
		return nil, err
	}

	return ParseWithBytes(buf)
}

func ParseWithBytes(buf []byte) (*config.Config, error) {
	return config.Parse(buf)
}
